<%-- 
    Document   : addContent
    Created on : Jun 6, 2008, 2:05:10 PM
    Author     : kristof
--%>
<%@ page import="POJO.*" %>
<%@ page import="DB.*" %>
<%@ page import="java.io.*" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%
            String username = ((User) session.getAttribute("user")).username;

            String type = request.getParameter("type");
            String containerID = request.getParameter("containerid");
            String contentID = request.getParameter("elementid");
            String ytlink = request.getParameter("ytlink");
            String blogpost = request.getParameter("blogpost");
            String contentType = request.getContentType();
            if (type.compareTo("image") == 0 && containerID != null && username != null) {
                ContentContainerManager ccm = new ContentContainerManager(DBInfo.getConnection());
                if (ccm.containerIsOwnedByUser(Integer.parseInt(containerID), username)) {
                    if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
                        DataInputStream in = new DataInputStream(request.getInputStream());
                        //we are taking the length of Content type data
                        int formDataLength = request.getContentLength();
                        byte dataBytes[] = new byte[formDataLength];
                        int byteRead = 0;
                        int totalBytesRead = 0;
                        //this loop converting the uploaded file into byte code
                        while (totalBytesRead < formDataLength) {
                            byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                            totalBytesRead += byteRead;
                        }

                        String file = new String(dataBytes);
                        //for saving the file name
                        String saveFile = file.substring(file.indexOf("filename=\"") + 10);
                        saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
                        saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
                        int lastIndex = contentType.lastIndexOf("=");
                        String boundary = contentType.substring(lastIndex + 1, contentType.length());
                        int pos;
                        //extracting the index of file 
                        pos = file.indexOf("filename=\"");
                        pos = file.indexOf("\n", pos) + 1;
                        pos = file.indexOf("\n", pos) + 1;
                        pos = file.indexOf("\n", pos) + 1;
                        int boundaryLocation = file.indexOf(boundary, pos) - 4;
                        int startPos = ((file.substring(0, pos)).getBytes()).length;
                        int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

                        // creating a new file with the same name and writing the content in new file

                        ContentManager cm = new ContentManager(DBInfo.getConnection());
                        int nextID = cm.getLatestContendID() + 1;
                        String fileName = "/opt/apache-tomcat-6.0.16/webapps/gwtgroep8.didactiek1.edm.uhasselt.be/web/uploads/" + Integer.toString(nextID) + saveFile;

                        if (fileName.toLowerCase().endsWith(".png") || fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".jpeg") || fileName.toLowerCase().endsWith(".bmp") || fileName.toLowerCase().endsWith(".gif") || fileName.toLowerCase().endsWith(".svg") || fileName.toLowerCase().endsWith(".xcf")) {
                            FileOutputStream fileOut = new FileOutputStream(fileName);
                            fileOut.write(dataBytes, startPos, (totalBytesRead - startPos));
                            fileOut.flush();
                            fileOut.close();
                            File temp = new File(fileName);
                            flickrAPI flickr = new flickrAPI();
                            if (flickr.upload(saveFile, temp, Integer.parseInt(containerID))) {
                                temp.delete();
%>
<div class="nice">
    <h1 class="nice">Image added successfully.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div> 

<% } else if (cm.addImage("uploads/" + Integer.toString(nextID) + saveFile, Integer.parseInt(containerID))) {
%>
<div class="nice">
    <h1 class="nice">Image added successfully.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div> 

<%} else {%>
<div class="nice">
    <h1 class="nice">Failed uploading image.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div> 

<% }
            } else {%>
<div class="nice">
    <h1 class="nice">File specified wasn't an image file.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div> 
<%    }
        }

    }
} else if (type.compareTo("video") == 0 && containerID != null && username != null) {

    ContentContainerManager ccm = new ContentContainerManager(DBInfo.getConnection());
    if (ccm.containerIsOwnedByUser(Integer.parseInt(containerID), username)) {
        ContentManager cm = new ContentManager(DBInfo.getConnection());
        if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
            DataInputStream in = new DataInputStream(request.getInputStream());
            //we are taking the length of Content type data
            int formDataLength = request.getContentLength();
            byte dataBytes[] = new byte[formDataLength];
            int byteRead = 0;
            int totalBytesRead = 0;
            //this loop converting the uploaded file into byte code
            while (totalBytesRead < formDataLength) {
                byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                totalBytesRead += byteRead;
            }

            String file = new String(dataBytes);
            //for saving the file name
            String saveFile = file.substring(file.indexOf("filename=\"") + 10);
            saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
            saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
            int lastIndex = contentType.lastIndexOf("=");
            String boundary = contentType.substring(lastIndex + 1, contentType.length());
            int pos;
            //extracting the index of file 
            pos = file.indexOf("filename=\"");
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            int boundaryLocation = file.indexOf(boundary, pos) - 4;
            int startPos = ((file.substring(0, pos)).getBytes()).length;
            int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

            // creating a new file with the same name and writing the content in new file

            int nextID = cm.getLatestContendID() + 1;
            String fileName = "/opt/apache-tomcat-6.0.16/webapps/gwtgroep8.didactiek1.edm.uhasselt.be/web/uploads/" + Integer.toString(nextID) + saveFile;

            if (fileName.toLowerCase().endsWith(".avi") || fileName.toLowerCase().endsWith(".flv") || fileName.toLowerCase().endsWith(".mpg")) {
                FileOutputStream fileOut = new FileOutputStream(fileName);
                fileOut.write(dataBytes, startPos, (totalBytesRead - startPos));
                fileOut.flush();
                fileOut.close();
                String flvSaveFile = saveFile.replaceAll(".avi", ".flv");
                flvSaveFile = flvSaveFile.replaceAll(".mpg", ".flv");
                if (cm.addVideo(Integer.toString(nextID) + flvSaveFile, Integer.parseInt(containerID))) {
                    if (!fileName.toLowerCase().endsWith(".flv")) {
                        String flvFilename = "/opt/apache-tomcat-6.0.16/webapps/gwtgroep8.didactiek1.edm.uhasselt.be/web/uploads/" + Integer.toString(nextID) + flvSaveFile;
                        try {
                            Runtime.getRuntime().exec("ffmpeg -i " + fileName + " " + flvFilename);
                        //Runtime.getRuntime().exec("rm " + fileName);
                        } catch (IOException ex) {
                            System.out.println("IOException while converting flash movie: " + ex.getMessage());
                        }
                    }
%>
<div class="nice">
    <h1 class="nice">Video added successfully.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div> 

<%} else {%>
<div class="nice">
    <h1 class="nice">Failed uploading video.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div> 

<% }
    } else {%>
<div class="nice">
    <h1 class="nice">File specified wasn't a video file.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div> 
<%                      }
} else if (cm.addVideo(ytlink, Integer.parseInt(containerID))) {%>
<div class="nice">
    <h1 class="nice">Video added successfully.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div>            
<%
} else {%>
<div class="nice">
    <h1 class="nice">Failed adding video.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div>  
<%    }
    }
} else if (type.compareTo("blogpost") == 0 && containerID != null && username != null) {
    ContentContainerManager ccm = new ContentContainerManager(DBInfo.getConnection());
    if (ccm.containerIsOwnedByUser(Integer.parseInt(containerID), username)) {
        ContentManager cm = new ContentManager(DBInfo.getConnection());
        if (cm.addBlogpost(blogpost, Integer.parseInt(containerID))) {%>
<div class="nice">
    <h1 class="nice">Blog post added successfully.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div>
<%
    } else {%>
<div class="nice">
    <h1 class="nice">Failed adding blog post.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div>  

<%  }
    }
} else if (type.compareTo("editblogpost") == 0 && containerID != null && contentID != null && username != null) {
    ContentContainerManager ccm = new ContentContainerManager(DBInfo.getConnection());
    ContentManager cm = new ContentManager(DBInfo.getConnection());
    if (ccm.containerIsOwnedByUser(Integer.parseInt(containerID), username) && cm.getContainerID(Integer.parseInt(contentID)) == Integer.parseInt(containerID)) {
        if (cm.changeBlogpost(blogpost, Integer.parseInt(contentID))) {%>
<div class="nice">
    <h1 class="nice">Blog post changed successfully.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div>
<%
    } else {%>
<div class="nice">
    <h1 class="nice">Failed changing blog post.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;Click <a href="index.jsp?page=triplocation&amp;containerid=<% out.print(containerID);%>">here</a> to go back to the trip location.
</div>  
<%  }
    }
}
%>